package com.fingard.dsp.bank.directbank.fngrd01;

import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.crypto.DigestHelper;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IAppOrder;
import com.fingard.dsp.bank.directbank.fngrd01.util.HttpUtils;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqUnifiedOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsUnifiedOrder;
import com.fingard.text.StringHelper;

import java.util.Date;
import java.util.HashMap;

/**
 * 融易保APP支付
 * 
 * @author Cruise
 * @date 2019/3/12
 */
public class FnGrd01AppOrder extends FnGrd01Base implements IAppOrder {
	@Override
	public RetAtsUnifiedOrder submitAppOrder(ReqUnifiedOrder p_req) {
        return null;
    }

	@Override
	public RetAtsQueryOrder queryAppPay(ReqQueryOrder p_req) {
        RetAtsQueryOrder retAts = new RetAtsQueryOrder();
        retAts.rdSeq = p_req.rdSeq;

        try {
        	ActSetItem actItem = getBankConfig().getActSetItem(p_req.ownAct, "appOrder");
            if (actItem != null) {
            	HashMap<String, Object> reqMap = new HashMap<String, Object>();

                reqMap.put("enterpriseNum", actItem.inMerchantId);
                reqMap.put("method", "queryOrder");
                reqMap.put("format", "JSON");
                reqMap.put("charset", getCharset());
                reqMap.put("signType", "MD5");
                reqMap.put("transSeqID", Format.DateTime17SSSFormat.format(new Date()));
                reqMap.put("version", "1.0");
                
                HashMap<String, String> bizContentMap = new HashMap<String, String>();
                bizContentMap.put("exchangeNo", p_req.rdSeq);
                bizContentMap.put("fgTradeNo", "");
                bizContentMap.put("mchId", "");
                bizContentMap.put("systemType", "UNKNOW");
                reqMap.put("bizContent", bizContentMap);
                
                String signStr = oriSignStr(reqMap, actItem.saltKey);
                WriteBankLogLn2("签名原始数据:\r\n" + signStr);
//                System.out.println("签名原始串:\r\n" + signStr);
                
                String signature = DigestHelper.cryptoMD5RetHexStr(signStr.getBytes(getCharset()));
                reqMap.put("sign", signature);
                String reqPostJson = getPostJson(reqMap);
                WriteBankLogStep2(reqPostJson);
//                System.out.println("请求数据:\r\n" + reqPostJson);
                
        		String tmpStrRet = HttpUtils.doPost(bankFront.getUrl("appOrderQry"), "", reqPostJson.getBytes(getCharset()), 50000, 50000, "", 0);
        		WriteBankLogStep3(tmpStrRet);
//        		System.out.println("返回数据:\r\n"+tmpStrRet);
        		
        		JSONObject tmpRespJson = JSONObject.parseObject(tmpStrRet);
        		
                retAts.respCode = "0";
                retAts.respInfo = tmpRespJson.getString("msg");
                
                String tmpRespCode = tmpRespJson.getString("code");
                if ("0".equals(tmpRespCode)) {
                	String bizCode = tmpRespJson.getString("bizCode");
                	String bizMsg = tmpRespJson.getString("bizMsg");
                	if (StringHelper.hasAnyChar(bizMsg)) {
                		retAts.respInfo = bizMsg;
                	}
                    retAts.transState = StringHelper.joinNotEmpty("+", bizCode, tmpRespJson.getString("tradeState"));
                    retAts.payInfoCode = retAts.transState;
                    String outTransID = tmpRespJson.getString("transactionId");
                    if (StringHelper.isNullOrEmpty(outTransID)) {
                        outTransID = tmpRespJson.getString("fgTradeNo");
                    }
                    retAts.oppAct = tmpRespJson.getString("account");
                    retAts.outTransID = outTransID;
                    retAts.oppBank = tmpRespJson.getString("bankType");
                    retAts.openId = tmpRespJson.getString("userId");
                    retAts.ourBankAccount = tmpRespJson.getString("mchId");
                } else {
                	retAts.respCode = "-1";
                    retAts.respInfo = tmpRespJson.getString("msg");
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号["+ p_req.ownAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return retAts;
    }
}
